package org.lagou.edu.stock.service;

import com.alibaba.dubbo.config.annotation.Service;
import org.lagou.edu.rpc.IStockService;
import org.lagou.edu.stock.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Author 咖啡杯里的茶
 * @date 2022/7/16
 */
@Service(interfaceClass = IStockService.class)
@Component
public class StockService implements IStockService {


	@Autowired
	private StockMapper stockMapper;

	/**
	 * 扣减库存
	 * @param shopId 商品id
	 * @param quantity 扣减的数量
	 * @return
	 */
	@Override
	public boolean deductionStock(long shopId, long quantity) {
		// 从mapper层中调用扣减方法。结果为0代表扣减失败，结果非0代表扣减成功
		int res = stockMapper.deduce(shopId, quantity);
		if (res == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获取当前存量
	 * @param shopId 商品id
	 * @return
	 */
	@Override
	public long queryStock(long shopId) {
		// 从mapper层中直接调用获取数量方法
		return stockMapper.getQuantity(shopId);
	}
}
